home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C++ / Applications / PICSee Dust 1.01 / Secondary Source / PICS_Split.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-15  |  7.8 KB  |  282 lines  |  [TEXT/CWIE]

  1. #include "PICS_Operations.h"
  2. #include "PICS_Split.h"
  3. #include "PICS_Types.h"            // Creator, file types
  4. #include "PICS_Utils.h"
  5.  
  6. #include "FileRegistry.h"
  7. #include "Class_ProgressWindow.h"
  8. #include "Monitors.h"
  9. #include "SavePicture.h"        // Save a PICT resource as a PICT file
  10. #include "StringUtils.h"        // Append, manipulate filenames
  11.  
  12. #include "main.h"                // For AppEmergencyUpdate()
  13.  
  14. #include "SimpleError.h"        // For error-handling
  15. #include "assert_mac.h"
  16.  
  17. // ---------------------------------------------------------------------------
  18.  
  19. void SetupPICSFileSplit(FSSpec *picsFile) {
  20.     FSSpec    file;
  21.  
  22.     if (picsFile == NULL) {
  23.         // If we're passed NULL, do a StandardGetFile...
  24.         if (!OpenPICSFile(&file)) return;
  25.     }
  26.     else {
  27.         // Else we already have a file ready for us...
  28.         file = *picsFile;
  29.     }
  30.  
  31.     FInfo fileInfo;
  32.     OSErr myErr;
  33.     myErr = FSpGetFInfo(&file, &fileInfo);
  34.     if (myErr == noErr && fileInfo.fdType == kPICTFileType) {
  35.         SimpleError(kAlertErrID, kErrMsgID, kDontAcceptPICTFileErrMsg);
  36.         return;
  37.     }
  38.  
  39.     // Always save split PICT files with the Photoshop creator type
  40.     SplitPICSFile(kPhotoShopCreatorType, &file, -1);
  41. } // END SetupPICSFileSplit
  42.  
  43. // ---------------------------------------------------------------------------
  44.  
  45. void SplitPICSFile(OSType creatorType, FSSpec *picsFile, short fileRefNum) {
  46.     short    oldRefNum;
  47.     Boolean isOpenedAlready;
  48.     ProgressWindowPtr progressWindow;
  49.  
  50.     // We'll still need to use the <picsFile> argument, even if
  51.     // we were passed a valid <fileRefNum>
  52.     ASSERT(picsFile != NULL);
  53.  
  54.     oldRefNum = CurResFile();
  55.  
  56.     if (fileRefNum == -1) {
  57.         ASSERT(picsFile != NULL);
  58.         fileRefNum = FSpOpenResFile(picsFile, fsRdWrPerm);
  59.         if (!RegisterFile(fileRefNum)) {
  60.             UseResFile(oldRefNum);
  61.             SimpleError(kAlertErrID, kErrMsgID, kCantOpenFileErrMsg);
  62.             return;
  63.         }
  64.         isOpenedAlready = false;
  65.     }
  66.     else {
  67.         isOpenedAlready = true;
  68.     }
  69.  
  70.     if (fileRefNum != -1) {
  71.         short numPicts;
  72.         PicHandle picHandle;
  73.         Str15 indexStr;
  74.         Str15 suffixStr = kSplitSuffix;
  75.         Str63 fileName;
  76.  
  77.         UseResFile(fileRefNum);
  78.  
  79.         numPicts = Count1Resources(kPICSRsrcType);
  80.         if (numPicts == 0) {
  81.             UseResFile(oldRefNum);
  82.             SimpleError(kAlertErrID, kErrMsgID, kNoPICTsInFileErrMsg);
  83.             if (!isOpenedAlready) {
  84.                 UseResFile(oldRefNum);
  85.                 CloseResFile(fileRefNum);
  86.             }
  87.             return;
  88.         }
  89.         else {
  90.             UseResFile(oldRefNum);
  91.             NumToString(numPicts, indexStr);
  92.             ParamText("\pThe number of frames in this PICS file:",
  93.                 indexStr, NULL, NULL);
  94.  
  95.             // Ask user to proceed or not. If cancel, exit...
  96.             if (Alert(kAlertMsgID, NULL) == cancel) {
  97.                 if (!isOpenedAlready) {
  98.                     UseResFile(oldRefNum);
  99.                     CloseResFile(fileRefNum);
  100.                 }
  101.                 UseResFile(oldRefNum);
  102.                 return;
  103.             }
  104.         }
  105.  
  106.         UseResFile(fileRefNum);
  107.         AppEmergencyUpdate();
  108.  
  109.         // Assumes id starts at 128, and all PICTs are sequentially id
  110.         for (short i = 1, resID = kPICSRsrcStartID; i <= numPicts; i++, resID++) {
  111.             SetCursor(*GetCursor(watchCursor));
  112.  
  113.             picHandle = (PicHandle)Get1Resource(kPICSRsrcType, resID);
  114.             if (picHandle == NULL) {
  115.                 if (i > 1)
  116.                     delete progressWindow;
  117.                 UseResFile(oldRefNum);
  118.                 SimpleError(kAlertErrID, kErrMsgID, kUnableLoadPICTErrMsg);
  119.                 if (!isOpenedAlready) {
  120.                     CloseResFile(fileRefNum);
  121.                 }
  122.                 UseResFile(oldRefNum);
  123.                 return;
  124.             }
  125.  
  126.             DetachResource((Handle)picHandle);
  127.  
  128.             /*
  129.                 Here we make it convenient for the user (especially if there's
  130.                 a lot of frames). For the first exported frame, we ask the
  131.                 user for the location to save it, via StandardPutFile. From
  132.                 this file we get the necessary FSSpec data.
  133.                 Then on we save all the files by making the necessary dest
  134.                 FSSpec, thus removing the necessity for asking the user a
  135.                 StandardPutFile for every exported frame...
  136.             */
  137.             StandardFileReply reply;
  138.             OSErr osErr;
  139.             Str31 baseFileName;
  140.             if (i == 1) {
  141.                 // -------------------------------------
  142.                 // ADJUST FILENAME & APPEND FRAME NUMBER
  143.                 // -------------------------------------
  144.                 PStrCpy(picsFile->name, fileName);
  145.                 // Make sure filename is long enough to hold our suffixes;
  146.                 // else crop it to make it fit...
  147.                 // Here we give it enough for one char (•) and three digits
  148.                 if (fileName[0] > 27)
  149.                     fileName[0] = 27;
  150.                 PStrCat(suffixStr, fileName);
  151.                 NumToString(i, indexStr);
  152.                 PStrCat(indexStr, fileName);
  153.  
  154.                 // ----------------------------------
  155.                 // GET FIRST EXPORTED FRAME LOCATION
  156.                 // ----------------------------------
  157.                 SetCursor(&qd.arrow);
  158.                 StandardPutFile("\pSave first PICT file into:", fileName, &reply);
  159.                 if (!reply.sfGood) {
  160.                     if (!isOpenedAlready) {
  161.                         DisposeHandle((Handle)picHandle);
  162.                         CloseResFile(fileRefNum);
  163.                     }
  164.                     UseResFile(oldRefNum);
  165.                     return;
  166.                 }
  167.                 SetCursor(*GetCursor(watchCursor));
  168.                 
  169.                 AppEmergencyUpdate();
  170.  
  171.                 /*
  172.                     Another "smart" feature... We supply the user with the
  173.                     filename suffixed by "•1". If the user should change 
  174.                     the name, we'll use that instead for all the exported
  175.                     PICTs.
  176.                 */
  177.                 PStrCpy(reply.sfFile.name, baseFileName);
  178.                 if (baseFileName[baseFileName[0]] == '1' &&
  179.                     baseFileName[baseFileName[0]-1] == suffixStr[1]) {
  180.                     baseFileName[0] = baseFileName[0] - 2;
  181.                 }
  182.                 if (baseFileName[0] > 27)
  183.                     baseFileName[0] = 27;
  184.  
  185.                 // Show progress window
  186.                 progressWindow = new ProgressWindow(GetDeepestDevice(), numPicts);
  187.                 SetPort(progressWindow->GetWindow());
  188.                 TextFont(geneva);
  189.                 TextSize(9);
  190.                 TextFace(bold);
  191.             }
  192.  
  193.             progressWindow->SetPrimaryMessage("\pSplitting PICS file...");
  194.             progressWindow->Increment();
  195.  
  196.             PStrCpy(baseFileName, fileName);
  197.             PStrCat(suffixStr, fileName);
  198.             NumToString(i, indexStr);
  199.             PStrCat(indexStr, fileName);
  200.             
  201.  
  202.             // ----------------------------------
  203.             // MAKE NEW DESTINATION FSSPEC
  204.             // ----------------------------------
  205.             FSSpec destFile;
  206.             osErr = FSMakeFSSpec(reply.sfFile.vRefNum, reply.sfFile.parID,
  207.                 fileName, &destFile);
  208.             // Note: FSMakeFSSpec will return an fnfErr error code, but
  209.             // the FSSpec is still valid.
  210.             if (osErr != noErr && osErr != fnfErr) {
  211.                 delete progressWindow;
  212.                 if (!isOpenedAlready) {
  213.                     DisposeHandle((Handle)picHandle);
  214.                     CloseResFile(fileRefNum);
  215.                 }
  216.                 UseResFile(oldRefNum);
  217.                 SimpleError(kAlertErrID, kErrMsgID, kCantMakeFSSpecErrMsg);
  218.                 return;
  219.             }
  220.  
  221.             // --------------------------------------
  222.             // IF FILE EXISTS, UNILATERALLY DELETE IT
  223.             // --------------------------------------
  224.             short refNum;
  225.             osErr = FSpOpenDF(&destFile, fsCurPerm, &refNum);
  226.             if (osErr == noErr) {
  227.                 // We were able to open the file, so means that
  228.                 // it exists (duh!) Close it and delete it...
  229.  
  230.                 progressWindow->SetPrimaryMessage("\pDeleting previous file...");
  231.                 progressWindow->SetSecondaryMessage(destFile.name);
  232.  
  233.                 FSClose(refNum);
  234.                 osErr = FSpDelete(&destFile);
  235.                 if (osErr != noErr) {
  236.                     delete progressWindow;
  237.                     if (!isOpenedAlready) {
  238.                         DisposeHandle((Handle)picHandle);
  239.                         CloseResFile(fileRefNum);
  240.                     }
  241.                     UseResFile(oldRefNum);
  242.                     SimpleError(kAlertErrID, kErrMsgID, kCantDeleteFileErrMsg);
  243.                     return;
  244.                 }
  245.             }
  246.  
  247.             // ----------------------------------
  248.             // CREATE PICT FILE
  249.             // ----------------------------------
  250.             osErr = FSpCreate(&destFile, creatorType,
  251.                 kPICTFileType, reply.sfScript);
  252.             progressWindow->SetPrimaryMessage("\pSaving PICT file...");
  253.             progressWindow->SetSecondaryMessage(destFile.name);
  254.  
  255.             if (osErr == noErr && !SavePictureFile(&destFile, ((Handle)picHandle))) {
  256.                 delete progressWindow;
  257.                 if (!isOpenedAlready) {
  258.                     DisposeHandle((Handle)picHandle);
  259.                     CloseResFile(fileRefNum);
  260.                 }
  261.                 UseResFile(oldRefNum);
  262.                 SimpleError(kAlertErrID, kErrMsgID, kCantCreateFileErrMsg);
  263.                 return;
  264.             }
  265.             
  266.             DisposeHandle((Handle)picHandle);
  267.         }
  268.  
  269.         delete progressWindow;
  270.         FlushEvents(everyEvent, 0);    // In case mouse was clicked during progress window
  271.  
  272.         if (!isOpenedAlready) {
  273.             UseResFile(oldRefNum);
  274.             CloseResFile(fileRefNum);
  275.         }
  276.     }
  277.     else {
  278.         SimpleError(kAlertErrID, kErrMsgID, kCantOpenFileErrMsg);
  279.     }
  280.     
  281.     UseResFile(oldRefNum);
  282. } // END SplitPICSFile